﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Typing_GUI.CLASS
{
  
    public class CBaiTap
    {
       
        #region"Thuoc tinh va properties"
        private string tenbaitap;

        public string Tenbaitap
        {
            get { return tenbaitap; }
            set { tenbaitap = value; }
        }
        private string noidung;

        public string Noidung
        {
            get { return noidung; }
            set { noidung = value; }
        }
        private string mota;

        public string Mota
        {
            get { return mota; }
            set { mota = value; }
        }
        private int mabaitap;

        public int Mabaitap
        {
            get { return mabaitap; }
            set { mabaitap = value; }
        }
        private int capdobaitap;

        public int Capdobaitap
        {
            get { return capdobaitap; }
            set { capdobaitap = value; }
        }
        private int loaibaitap;

        public int Loaibaitap
        {
            get { return loaibaitap; }
            set { loaibaitap = value; }
        }
        private int diemtoida;

        public int Diemtoida
        {
            get { return diemtoida; }
            set { diemtoida = value; }
        }
        private int thoigian;

        public int Thoigian
        {
            get { return thoigian; }
            set { thoigian = value; }
        }
        private int nguoisoan;

        public int Nguoisoan
        {
            get { return nguoisoan; }
            set { nguoisoan = value; }
        }

        private bool tinhTrang;

        public bool TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }
    #endregion

     

        #region "Phương thức"
        public static DataTable GetLessonList()
        {
            DataTable dt = new DataTable();
            string sql = "select*from BaiTap";
            dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }

        public static DataTable GetLessonListFull()
        {
            DataTable dt = new DataTable();
            string sql = "select  bt.mabaitap,bt.tenbaitap,bt.noidung,bt.mota,bt.thoigian,bt.diemtoida,";
            sql += "cd.tencapdo,l.tenloai,nd.tendangnhap,bt.tinhtrang,bt.capdobaitap,bt.loaibaitap,bt.nguoisoan ";
            sql += "from BaiTap bt,nguoidung nd,capdobaitap cd,loaibaitap l,loainguoidung lnd ";
            sql += "where bt.nguoisoan=nd.manguoidung and bt.capdobaitap=cd.macapdobaitap";
            sql += " and bt.loaibaitap=l.maloai and lnd.maloai=nd.loainguoidung";
            dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }

        public static List<CBaiTap> GetLessonListByLevel(int level,int lessonType)
        {
            List<CBaiTap> LessonList = new List<CBaiTap>();
            string sql = "select*from BaiTap where LoaiBaiTap="+lessonType.ToString()+" and TinhTrang=True and  CapDoBaiTap=" + level.ToString();
            DataTable dt = DataProvider.ExecuteQuery(sql);
            CBaiTap lesson;
            foreach (DataRow row in dt.Rows)
            {
                lesson = new CBaiTap();
                lesson.Mabaitap = int.Parse(row["MaBaiTap"].ToString());
                lesson.Tenbaitap = row["TenBaiTap"].ToString();
                lesson.Noidung = row["NoiDung"].ToString();
                lesson.Mota = row["MoTa"].ToString();
                lesson.Thoigian = int.Parse(row["ThoiGian"].ToString());
                lesson.Diemtoida = int.Parse(row["DiemToiDa"].ToString());
                lesson.Capdobaitap = int.Parse(row["CapDoBaiTap"].ToString());
                lesson.Loaibaitap = int.Parse(row["LoaiBaiTap"].ToString());
                lesson.Nguoisoan = int.Parse(row["NguoiSoan"].ToString());
                lesson.TinhTrang = Boolean.Parse(row["TinhTrang"].ToString());
                LessonList.Add(lesson);
            }
            return LessonList;
        }
        public static DataTable GetLesson(int id)
        {
            DataTable dt = new DataTable();
            string sql = "select * from BaiTap where MaBaiTap=" + id.ToString();
            dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }
        public static DataTable GetLessonUser (int id)
        {
            DataTable dt = new DataTable();
            string sql = "select * from BaiTap where NguoiSoan=" + id.ToString();
            dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }

        public bool CreateLesson()
        {
            string sql = "insert into BaiTap(TenBaiTap,NoiDung,MoTa,ThoiGian,DiemToiDa,CapDoBaiTap,LoaiBaiTap,NguoiSoan,TinhTrang)";
            sql+=" values('" + this.tenbaitap + "','" + this.noidung + "','" + this.mota + "',";
            sql += this.thoigian.ToString() + "," + this.diemtoida.ToString() + "," + this.capdobaitap.ToString() + ",";
            sql+= this.loaibaitap.ToString() + "," + this.nguoisoan.ToString() + "," + this.tinhTrang.ToString() + ")";
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }
        public static bool DeleteLesson(int id)
        {
            string sql = "delete from BaiTap where MaBaiTap=" + id.ToString();
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }
        public bool UpdateLesson()
        {
            string sql = "update BaiTap set ";
            sql += "TenBaiTap='" + this.tenbaitap + "',";
            sql += "NoiDung='" + this.noidung + "',";
            sql += "MoTa='" + this.mota + "',";
            sql += "ThoiGian=" + this.thoigian.ToString() + ",";
            sql += "DiemToiDa=" + this.diemtoida.ToString() + ",";
            sql += " CapDoBaiTap=" + this.capdobaitap.ToString() + ",";
            sql += "LoaiBaiTap=" + this.loaibaitap.ToString() + ",";
            sql += "TinhTrang=" + this.tinhTrang.ToString();
            sql += ",nguoisoan=" + this.nguoisoan.ToString();
            sql += " where MaBaiTap =" + this.mabaitap.ToString();
            
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }
        //Ham tra ra so ky cho 1 diem
        public  int CalculateScore()
        {
            string[] str = this.noidung.Split(new string[] { "[P]" }, StringSplitOptions.RemoveEmptyEntries);
            int characterCount = 0;
            for (int i = 0; i < str.Length; i++)
            {
                characterCount += str[i].Length;
            }
            if (diemtoida == 0)
                diemtoida = 1;
            float temp= characterCount/diemtoida;
            if (temp == 0)
                temp = 1;
            return (int)temp;
        }

        //Ham lấy 1 bài tập
        public static CBaiTap GetLessonByID(int id)
        {
            CBaiTap result = new CBaiTap();
            DataTable dt = GetLesson(id);
            DataRow dr = dt.Rows[0];
            result.mabaitap = int.Parse(dr["MaBaiTap"].ToString());
            result.tenbaitap = dr["TenBaiTap"].ToString();
            result.Noidung = dr["NoiDung"].ToString();
            result.Mota = dr["MoTa"].ToString();
            result.Thoigian = int.Parse(dr["ThoiGian"].ToString());
            result.diemtoida = int.Parse(dr["DiemToiDa"].ToString());
            result.Capdobaitap = int.Parse(dr["CapDoBaiTap"].ToString());
            result.loaibaitap = int.Parse(dr["LoaiBaiTap"].ToString());
            result.Nguoisoan = int.Parse(dr["NguoiSoan"].ToString());
            result.tinhTrang = bool.Parse(dr["TinhTrang"].ToString());
            return result;
        }

        //Hàm đếm số lượng từ trong bài tập

        public static int WordsInLesson(CBaiTap lesson)
        {
            int wordCount = 0;
            string[] str = lesson.Noidung.Split(new string[] { "[P]" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < str.Length; i++)
            {
                string str1 = str[i].Trim();
                for (int j = 0; j < str1.Length; j++)
                {
                    if (str1[j] == ' ')
                        wordCount++;
                }
            }
            return wordCount;
        }

        public static DataTable LoadDanhSachBaiTap(int type)
        {
            DataTable dt = new DataTable();
            string sql = "";
            sql += " Select MaBaiTap,TenBaiTap ";
            sql += " From BaiTap ";
            sql += " Where TinhTrang=True and LoaiBaiTap ="+type.ToString();
            dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }
        public static string RemoveQuote(string text)
        {
            return text.Replace('\'','a');
        }
        #endregion


    }
}
